﻿package com.badibadi.activity;

import java.util.HashMap;
import java.util.Map;

import com.badibadi.infos.Results;
import com.badibadi.infos.User_HideModel;
import com.badibadi.mytools.Constants;
import com.badibadi.mytools.JSONUtils;
import com.badibadi.mytools.MyThreadTool;
import com.badibadi.mytools.Utils;
import com.badibadi.uniclubber.R;
import com.badibadi.uniclubber.R.layout;
import com.badibadi.uniclubber.R.menu;
import com.libjph.util.StringUtil;

import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.app.Activity;
import android.content.Intent;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.CheckBox;

public class PrivacySettingActivity extends BaseActivity implements OnClickListener{

	private String TAG="PrivacySettingActivity";
	private CheckBox Message_notice;
	private final int MSG_SUCCESSFUL=2;//成功
	private final int MSG_FAILURE=1;//失败
	private final int NETWORK_ERROR=3;//网络错误
	private final int MSG_INIT=4;//更改界面
	private final int MSG_SET_SUCCESSFUL=5;//设置成功后刷新一次界面
	private final String KEY="MSG_PrivacySettingActivity";
//	setKongjianPrivacy();
	CheckBox box0;
	CheckBox box1;
	CheckBox box2;
	User_HideModel user_HideModel;
	String id="26";
	private Handler handler = new Handler() {
		@Override
		public void handleMessage(Message msg) {
			switch (msg.what) {
			case 1:
				Utils.ExitPrgress(PrivacySettingActivity.this);
				try {
					Utils.showMessage(PrivacySettingActivity.this, getResources().getString(R.string.l_net_error));
				} catch (Exception e) {
					// TODO: handle exception
				}
				
				break;
			case MSG_SUCCESSFUL:
				Utils.ExitPrgress(PrivacySettingActivity.this);
				if((Boolean) msg.getData().get(KEY))
				{
					Utils.showMessage(PrivacySettingActivity.this,getResources().getString(R.string.l_xb55));
				}else
				{
					Utils.showMessage(PrivacySettingActivity.this,getResources().getString(R.string.l_xb113));
				}
				
				break;
			case NETWORK_ERROR:
				Utils.ExitPrgress(PrivacySettingActivity.this);
				try {
					Utils.showMessage(PrivacySettingActivity.this, getResources().getString(R.string.l_net_error));
				} catch (Exception e) {
					// TODO: handle exception
				}
				
				break;

			case MSG_INIT:
				Utils.ExitPrgress(PrivacySettingActivity.this);
				if(user_HideModel!=null)
				{
					if(!StringUtil.isNullOrEmpty(user_HideModel.getIs_inform()))
					{
						if(user_HideModel.getIs_inform().equals("0"))
						{
							Message_notice.setChecked(false);
						}else
						{
							Message_notice.setChecked(true);
						}
					}
					if(!StringUtil.isNullOrEmpty(user_HideModel.getPrivacy()))
					{
						if(user_HideModel.getPrivacy().equals("0"))
						{
							setPermissions(0);
						}else if(user_HideModel.getPrivacy().equals("1"))
						{
							setPermissions(1);
						}else if(user_HideModel.getPrivacy().equals("2"))
						{
							setPermissions(2);
						}
					}
				}
				break;
			case MSG_SET_SUCCESSFUL:
				initUser_hide();
				break;
			default:
				break;
			}
		}
	};
	
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		goToTheTitle();
//		android4Detail();
		setContentView(R.layout.privacy_setting);
	    id=Utils.getUid(PrivacySettingActivity.this);
		Message_notice=(CheckBox)this.findViewById(R.id.check_box_Message_notice);
		
//		//第一次初始化
		initUser_hide();//初始化设置消息通知的接口
		
		this.findViewById(R.id.return_btn).setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				finish();
			}
		});
		//设定俱乐部通知的按钮
		this.findViewById(R.id.club_Notice).setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				if(Message_notice.isChecked())
				{
					Message_notice.setChecked(false);
					setJIeshouNotice(0);
				}else if(!Message_notice.isChecked())
				{
					Message_notice.setChecked(true);
					setJIeshouNotice(1);
				}
			}
		});
       
		this.findViewById(R.id.all_people).setOnClickListener(this);
		this.findViewById(R.id.friends).setOnClickListener(this);
		this.findViewById(R.id.only_myself).setOnClickListener(this);
		
		box0=(CheckBox)findViewById(R.id.checkBox_all_friends);
		box1=(CheckBox)findViewById(R.id.checkBox_friends);
		box2=(CheckBox)findViewById(R.id.CheckBox_onlymyself);
	}
	private void initUser_hide() {
		// TODO Auto-generated method stub
		Utils.showPrgress(PrivacySettingActivity.this);
		MyThreadTool.fixedThreadPool.execute(new Runnable() {
			@Override
			public void run() {
				Map<String, Object> map = new HashMap<String, Object>();
				map.put("uid", id);
				String t_content = Utils.sendRequest(map, Constants.BadiUrl
						+ "user_hide");
				if (t_content != null) {
//					   
					Results results = Utils.checkResult_NNN(getApplicationContext(),
							t_content);
					if (results != null && !results.getRetmsg().equals("null")) {
						try {
							
							user_HideModel=JSONUtils.getEntityByJsonString(results.getRetmsg(), User_HideModel.class);
                        System.out.println("yinsisheding"+results.getRetmsg());
							handler.sendEmptyMessage(MSG_INIT);
						} catch (Exception e) {
							// TODO Auto-generated catch block
							   
						}
					} else {
						handler.sendEmptyMessage(3);
					}
				} else {
					handler.sendEmptyMessage(1);
				}
			}
		});
	}
	/**
	 * 设置俱乐部是否接收通知
	 * @param i 0不接收俱乐部通知，1接收俱乐部通知
	 */
	public void setJIeshouNotice(final int i) {
		// TODO Auto-generated method stub
		Utils.showPrgress(PrivacySettingActivity.this);
		MyThreadTool.fixedThreadPool.execute(new Runnable() {
			@Override
			public void run() {
				Map<String, Object> map = new HashMap<String, Object>();
				map.put("uid", id);
				map.put("is_inform", i);
				String t_content = Utils.sendRequest(map, Constants.BadiUrl
						+ "set_club_pd");
				if (t_content != null) {
//					   ("返回期间的活动是:" + t_content);
					Results results = Utils.checkResult_NNN(getApplicationContext(),
							t_content);
					if (results != null && !results.getRetmsg().equals("null")) {
						try {
                              Message msg=new Message();
                              msg.what=MSG_SUCCESSFUL;
                              Bundle bun=new Bundle();
                              bun.putBoolean(KEY, results.isRet());
                              msg.setData(bun);
							
							handler.sendMessage(msg);
						} catch (Exception e) {
							// TODO Auto-generated catch block
							   
						}
					} else {
						handler.sendEmptyMessage(3);
					}
				} else {
					handler.sendEmptyMessage(1);
				}
			}
		});
	}

	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
		switch(v.getId())
		{
		case R.id.all_people:
//			if(box0.isChecked()==true)
			setPermissions2(1);
			break;
		case R.id.friends:
//			if(box1.isChecked()==true)
			setPermissions2(2);
			break;
		case R.id.only_myself:
			
//			if(box2.isChecked()==true)
			setPermissions2(0);
			break;
		}
	}
	/**
	 * 设置空间的权限
	 */
	public void setKongjianPrivacy(final int privacy)
	{	
		MyThreadTool.fixedThreadPool.execute(new Runnable() {
			@Override
			public void run() {
				Map<String, Object> map = new HashMap<String, Object>();
				map.put("uid", id);
				if(privacy==0)
				map.put("privacy", "0");
				if(privacy==1)
					map.put("privacy", "1");
				if(privacy==2)
					map.put("privacy", "2");
				String t_content = Utils.sendRequest(map, Constants.BadiUrl
						+ "set_space_authority");
				if (t_content != null) {
//					   ("返回期间的活动是:" + t_content);
					Results results = Utils.checkResult_NNN(getApplicationContext(),
							t_content);
					System.out.println("kongjian"+results);
					if (results != null && !results.getRetmsg().equals("null")) {
						try {
                              Message msg=new Message();
                              msg.what=MSG_SET_SUCCESSFUL;
                              Bundle bun=new Bundle();
                              bun.putBoolean(KEY, results.isRet());
                              msg.setData(bun);
							
							handler.sendMessage(msg);
						} catch (Exception e) {
							// TODO Auto-generated catch block
							   
						}
					} else {
						handler.sendEmptyMessage(3);
					}
				} else {
					handler.sendEmptyMessage(1);
				}
			}
		});
	}
	/**
	 * 0仅自己 1所有人 2朋友
	 */
	private void setPermissions(int num){
		
//		setKongjianPrivacy(num);
		if(num==0)
		{
			box0.setChecked(false);
			box1.setChecked(false);
			box2.setChecked(true);
		}
		else if(num==1)
		{
			box0.setChecked(true);
			box1.setChecked(false);
			box2.setChecked(false);
		}
		else if(num==2)
		{
			box0.setChecked(false);
			box1.setChecked(true);
			box2.setChecked(false);
		}
		
	}
private void setPermissions2(int num){
		
		setKongjianPrivacy(num);
//		if(num==0)
//		{
//			box0.setChecked(false);
//			box1.setChecked(false);
//			box2.setChecked(true);
//		}
//		else if(num==1)
//		{
//			box0.setChecked(true);
//			box1.setChecked(false);
//			box2.setChecked(false);
//		}
//		else if(num==2)
//		{
//			box0.setChecked(false);
//			box1.setChecked(true);
//			box2.setChecked(false);
//		}
		
	}
}
